package com.itheima.server;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer;

import javax.websocket.server.ServerContainer;

// 主类启动WebSocket服务器
public class ChatServer {
    public static void main(String[] args) throws Exception {
        // 创建Jetty服务器，端口8080
        Server server = new Server(8080);

        // 创建Servlet上下文
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);

        // 配置WebSocket
        ServerContainer container = WebSocketServerContainerInitializer.configureContext(context);
        container.addEndpoint(ChatEndpoint.class);

        // 启动服务器
        System.out.println("科技聊天服务器启动在端口: 8080");
        server.start();
        server.join();
    }
}
